const http = require('../../../utils/http.js')
const Base64 = require('../../../utils/base64.js')
const wxparse = require("../../../wxParse/wxParse.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        inputArr: [],
        ifAgree: true,
        canClick: true,
        showPro: false
    },

    /**
     * 跳过
     */
    ignore() {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },

    /**
     * 返回
     */
    goBack() {
        wx.navigateBack({
            delta: 1
        })
    },

    /**
     *  获取所有INPUT的数据
     */
    inputTxt(e) {
        let inputArr = this.data.inputArr,
            temp = http.inputArr(inputArr, e)
        // console.log(temp)
        this.setData({
            inputArr: temp
        })
    },

    /**
     * 是否同意协议
     */
    changeAgreeStatus() {
        let that = this,
            ifAgree = that.data.ifAgree
        if (ifAgree) {
            that.setData({
                ifAgree: false
            })
        } else {
            that.setData({
                ifAgree: true
            })
        }
    },

    /**
     * 获取注册协议
     */
    getProtocol() {
        let that = this,
            url = 'User_protocol'

        let ifAgree = that.data.ifAgree
        that.setData({
            ifAgree: false
        })

        http._post(
            url,
            {},
            res => {
                // console.log(res.data)
                // let data = res.data
                // if (data.re === '1') {
                    that.setData({
                        // protocol: data.data,
                        protocol: res.data,
                        showPro: true
                    })
                    
                    wxparse.wxParse('protocol', 'html', http.getBody(that.data.protocol), that, 5)
                // } else {
                //     wx.showToast({
                //         title: data.info,
                //         icon: 'none',
                //         duration: 3000
                //     })
                // }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 关闭服务协议
     */
    closeProtocol() {
        this.setData({
            showPro: false
        })
    },

    /**
     * 到登录
     */
    goLogin() {
        wx.navigateBack({
            delta: 1
        })
        // if (this.data.type === '1') {
        //     wx.navigateTo({
        //         url: '/pages/login/login'
        //     })
        // } else {
        //     wx.navigateTo({
        //         url: '/pages/login/loginP/loginP'
        //     })
        // }
    },

    /**
     * 获取短信验证码
     */
    getVercoe() {
        console.log('验证啊')
        let that = this,
            url = 'User_getMessage',
            tel = that.data.inputArr[0]

        if (!http.ifEmpty(tel)) {
            wx.showToast({
                title: '请填写手机号',
                icon: 'none',
                duration: 3000
            })
            return false
        }

        if (!http.rexFn(tel)[0]) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
                duration: 3000
            })
            return false
        }

        // console.log(Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'))
        http._post(
            url,
            { mobile: Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'), type: 1},
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                    that.setData({
                        ifVerify: true,
                        timeDown: 60
                    })
                    let time = 60,
                        timeCount = setInterval(function () {
                        time--
                        that.setData({
                            timeDown: time
                        })
                        if (time <= 0) {
                            that.setData({
                                ifVerify: false
                            })
                            clearInterval(timeCount)
                        }
                    }, 1000)
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 注册
     */
    getRegister() {
        let that = this,
            url = 'User_register',
            inputArr = that.data.inputArr,
            mobile = Base64.CusBASE64.encoder('UNO' + inputArr[0] + 'HACHA'),
            tel = inputArr[0],
            code = inputArr[1],
            password = Base64.CusBASE64.encoder('UNO' + inputArr[2] + 'HACHA'),
            pstel = inputArr[2],
            city_code = inputArr[3],
            company_code = inputArr[4]
            console.log(tel)
            // if (!tel){
            //   wx.showToast({
            //     title: '请填写手机号',
            //     icon: 'none',
            //     duration: 3000
            // })
            //   return false
            // }
            // if (!code) {
            //   wx.showToast({
            //     title: '请获取验证码',
            //     icon: 'none',
            //     duration: 3000
            //   })
            //   return false
            // }
            if (!tel) {
              wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 3000
              })
              return false
            }
            if (tel<11) {
              wx.showToast({
                title: '请输入正确手机号',
                icon: 'none',
                duration: 3000
              })
              return false
            }
            if (!pstel) {
              wx.showToast({
                title: '请输入密码',
                icon: 'none',
                duration: 3000
              })
              return false
            }
            if (!http.ifEmpty(code)) {
                wx.showToast({
                    title: '请获取验证码',
                    icon: 'none',
                    duration: 3000
                })
                return false
            }

            if (!http.ifEmpty(password)) {
                wx.showToast({
                    title: '请设置密码',
                    icon: 'none',
                    duration: 3000
                })
                return false
            }

            if (!that.data.ifAgree) {
                wx.showToast({
                    title: '请先同意服务协议',
                })
                return false
            }

            if (that.data.type === '1') {

                if (!http.ifEmpty(city_code)) {
                    wx.showToast({
                        title: '请填写城市编码',
                        icon: 'none',
                        duration: 3000
                    })
                    return false
                }

            if (!http.ifEmpty(company_code)) {
                wx.showToast({
                    title: '请填写机构识别码',
                    icon: 'none',
                    duration: 3000
                })
                return false
            }
            if(password.length ==='6'){
              http._post(
                url,
                { mobile: mobile, code: code, password: password, city_code: city_code, company_code: company_code },
                res => {
                  console.log(res.data)
                  let data = res.data
                  if (data.re === '1') {
                    wx.setStorageSync('token', data.data.token)
                    wx.showToast({
                      // title: data.info,
                      icon: 'none',
                      duration: 3000,
                      success: function () {
                        setTimeout(() => {
                          wx.reLaunch({
                            url: '/pages/chooseIdentity',
                          })
                        }, 500)
                      }
                    })
                    that.setData({
                      canClick: false
                    })
                  } else {
                    wx.showToast({
                      title: data.info,
                      icon: 'none',
                      duration: 3000
                    })
                  }
                },
                err => {
                  console.log(err)
                }
              )
            }
       
        } else {
            http._post(
                url,
                { mobile: mobile, code: code, password: password },
                res => {
                    console.log(res.data)
                    let data = res.data
                    if (data.re === '1') {
                        wx.setStorageSync('token', data.data.token)
                        wx.showToast({
                            title: data.info,
                            icon: 'none',
                            duration: 3000,
                            success: function () {
                                setTimeout(() => {
                                    wx.reLaunch({
                                        url: '/pages/chooseIdentity',
                                    })
                                }, 500)
                            }
                        })
                        that.setData({
                            canClick: false
                        })
                    } else {
                        wx.showToast({
                            title: data.info,
                            icon: 'none',
                            duration: 3000
                        })
                    }
                },
                err => {
                    console.log(err)
                }
            )
        }
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let type = options.type ? options.type.toString() : '0'
        this.setData({
            type: type
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})